package net.sf.odinms.exttools.doclet.pages;

import java.io.IOException;
import java.util.List;
import java.util.Vector;
import net.sf.odinms.exttools.doclet.Doclet;

/**
 * Creates "index-all.html" with listings of symbols for each letter.
 */
public final class SingleIndex extends AbstractIndex {
	
	/**
	 * Generate one index page for all symbols in the Javadocs.
	 */
	public static void generateSingleIndexPage() {
		String filename = "index-all" + CONF.ext;
		try {
			System.out.println(CONF.propertyText("doclet.index"));
			new SingleIndex(filename);
		}
		catch (IOException exc) {
			throw Doclet.exception(exc, filename);
		}
	}
	
	/**
	 * Initialize and create the single index page. Calls
	 * {@link #generateCharacterList generateCharacterList()} for each letter in
	 * {@link xhtmldoclet.DocletConfig#indexBuilder CONF.indexBuilder}.
	 * 
	 * @param filename The desired name of the file (with extension).
	 * @throws IOException If {@link java.io.FileOutputStream} creation fails.
	 */
	private SingleIndex(String filename) throws IOException {
		super(filename);
		
		windowTitle =
				(CONF.windowtitle.length() > 0)
						? CONF.windowtitle
						: CONF.propertyText("IndexSingle");
		printXhtmlHeader();
		Character curLetter;
		for (Object obj : CONF.indexBuilder.elements()) {
			curLetter = (Character) obj;
			characterList(curLetter, CONF.indexBuilder.getMemberList(curLetter));
		}
		printXhtmlFooter();
		this.close();
	}
	
	/** Creates links to index sections for each letter with documented symbols. */
	protected void navWithinPage() {
		List<String> letters = new Vector<String>();
		for (Object letter : CONF.indexBuilder.elements()) {
			letters.add(linkToLabelHref(letter.toString(), "#" + letter));
		}
		print(open("td id=\"WithinPage\""));
		printUnorderedListWithLast(letters);
		println(close("td"));
	}
	
}
